<template>
  <div class="header">
    <div class="logo">
      <img src="@/assets/icon/logo.png" alt="Logo" style="height: 71px"/>
      <span>潍坊理工学院会议预约管理系统</span>
    </div>
    <!-- Logo 图片 -->
    <div class="info">
      <el-dropdown @command="handleCommand" v-if="userInfo.name != ''">
        <div class="avatar">
          <img
              src="../assets/avatar.png"
              alt="Avatar"
              style="height: 50px; width: 50px; border-radius: 50%"
          />
          <span style="margin-left: 10px; color: #fff">{{
              userInfo.name
            }}</span>
        </div>
        <template v-slot:dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="edit">修改信息</el-dropdown-item>
            <el-dropdown-item command="logout">退出</el-dropdown-item>
            <el-dropdown-item command="editPassword">修改密码</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <el-dialog
          title="修改用户信息"
          v-model="editDialogVisible"
      >
        <el-form :model="userInfo">
          <el-form-item label="用户名:">
            <el-input v-model="userInfo.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="所属校区:">
            <el-select v-model="userInfo.campus" placeholder="请选择" :disabled="role===0">
              <el-option
                  v-for="campus in campusList"
                  :key="campus.id"
                  :label="campus.campusName"
                  :value="campus.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="userInfo.name" autocomplete="off" :disabled="role===0"></el-input>
          </el-form-item>
          <el-form-item label="电话">
            <el-input v-model="userInfo.contact" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="userInfo.email" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <template v-slot:footer>
          <span class="dialog-footer">
            <el-button @click="editDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="updateUserInfo">保存</el-button>
          </span>
        </template>
      </el-dialog>
      <el-dialog
          title="修改密码"
          v-model="editPasswordDialogVisible"
          v-if="editPasswordDialogVisible"
          @close="resetPwdForm"
      >
        <el-form :model="pwdForm">
          <el-form-item label="用户名:">
            <el-input v-model="pwdForm.username" disabled></el-input>
          </el-form-item>
          <el-form-item label="输入旧密码：">
            <el-input type="password" show-password v-model="pwdForm.oldPwd" autocomplete="off"
                      placeholder="输入旧密码： "></el-input>
          </el-form-item>
          <el-form-item label="输入新密码：">
            <el-input type="password" show-password v-model="pwdForm.password" autocomplete="off"
                      placeholder="输入6-20位密码 "></el-input>
          </el-form-item>
          <el-form-item label="确认新密码：">
            <el-input type="password" show-password v-model="pwdForm.confirmPwd" autocomplete="off"
                      placeholder="确认新密码： "></el-input>
          </el-form-item>
        </el-form>
        <template v-slot:footer>
          <span class="dialog-footer">
            <el-button @click="resetPwdForm">取消</el-button>
            <el-button type="primary" @click="updatePwd">保存</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {UpdateMeInfo, UpdateUserPassword} from '@/api/index'

export default {
  data() {
    return {
      userInfo: {
        username: '',
        name: '',
        campus: '',
        contact: '',
        email: '',
      },
      editDialogVisible: false,
      editPasswordDialogVisible: false,
      pwdForm: {
        username: '',
        password: '',
        oldPwd: '',
        confirmPwd: ''
      },
    }
  },
  created() {
    this.loadUserInfo()
  },
  watch: {
    $route(to, from) {
      this.loadUserInfo()
    },
  },
  computed: {
    campusList() {
      return this.$store.getters.getCampusList
    },
    role() {
      return this.$store.getters.getRole
    },
  },
  methods: {
    enterFullScreen() {
      let element = document.documentElement
      if (element.requestFullscreen) {
        element.requestFullscreen()
      } else if (element.mozRequestFullScreen) {
        /* Firefox */
        element.mozRequestFullScreen()
      } else if (element.webkitRequestFullscreen) {
        /* Chrome, Safari & Opera */
        element.webkitRequestFullscreen()
      } else if (element.msRequestFullscreen) {
        /* IE/Edge */
        element.msRequestFullscreen()
      }
    },
    exitFullScreen() {
      if (document.exitFullscreen) {
        document.exitFullscreen()
      } else if (document.mozCancelFullScreen) {
        /* Firefox */
        document.mozCancelFullScreen()
      } else if (document.webkitExitFullscreen) {
        /* Chrome, Safari and Opera */
        document.webkitExitFullscreen()
      } else if (document.msExitFullscreen) {
        /* IE/Edge */
        document.msExitFullscreen()
      }
    },
    loadUserInfo() {
      let storedData = localStorage.getItem('WFITInfo')
      if (storedData) {
        this.userInfo = JSON.parse(storedData)
      }
    },
    handleCommand(command) {
      if (command === 'edit') {
        this.editUserInfo()
      } else if (command === 'logout') {
        this.logout()
      } else if (command === 'editPassword') {
        this.editPassword()
      }
    },
    editPassword() {
      this.editPasswordDialogVisible = true
      let storedData = localStorage.getItem('WFITInfo')
      if (storedData) {
        this.pwdForm.username = JSON.parse(storedData).username
      }
    },
    updatePwd() {
      UpdateUserPassword(this.pwdForm).then((response) => {
        if (response.code === 200) {
          this.$message.success('密码修改成功')
          this.editPasswordDialogVisible = false
        }
      })
    },
    editUserInfo() {
      this.editDialogVisible = true
    },
    updateUserInfo() {
      UpdateMeInfo(this.userInfo).then((response) => {
        if (response.code === 200) {
          this.$message.success('用户信息更新成功')
        } else {
          this.$message.error(response.msg || '更新用户信息失败')
        }
      })
      localStorage.setItem('WFITInfo', JSON.stringify(this.userInfo))
      this.editDialogVisible = false
    },
    logout() {
      localStorage.removeItem('WFITRole')
      localStorage.removeItem('WFITInfo')
      localStorage.removeItem('WFITToken')
      localStorage.removeItem('WFITCampus')
      this.$router.push('/login')
      location.reload()
    },
    resetPwdForm() {
      this.editPasswordDialogVisible = false
      this.pwdForm = {
        username: '',
        password: '',
        oldPwd: '',
        confirmPwd: ''
      }
    }
  },
}
</script>

<style lang="less" scoped>
@import '@/assets/style/EditMeetingRoom.less';

.logo {
  width: 750px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 24px;
  color: #fff;
}

.header {
  height: 100px;
  width: 100%;
  display: flex;
  justify-content: space-between;
  background: #2d447a;
  align-items: center;
}

.info {
  color: #ffffff;
  display: flex;
  height: 72px;
  align-items: center;
  width: 300px;
  justify-content: space-evenly;
}

.avatar {
  display: flex;
  align-items: center;
  cursor: pointer;
}
</style>
